#include "taskCall.h"

#include "Teleport.pb-c.h"
void CAreaTeleport::UseCall()
{
	//DEBUG_INFO_OUTPUT("fs:CGameData::CAreaTeleport 111\n");
	int sum = GameCore_read<int>(GameCore_GetGameBaseAddress() + AreaTeleportSum_BASE);
	sum++;
	if (sum < 0)
	{
		sum = 1;
	}
	GameCore_write<int>(GameCore_GetGameBaseAddress() + AreaTeleportSum_BASE, sum);
	if (sum > 0)
	{
		IM__PTeleport Teleport;
		im__pteleport__init(&Teleport);
		Teleport.id = m_id;

		Teleport.count = sum;
		size_t packed_size = protobuf_c_message_get_packed_size((const ProtobufCMessage*)&Teleport);

		if (packed_size)
		{
			std::shared_ptr<uint8_t[]> packedBuf(new uint8_t[packed_size], std::default_delete<uint8_t[]>());
			if (packedBuf)
			{
				im__pteleport__pack(&Teleport, packedBuf.get());
				//DEBUG_INFO_OUTPUT("fs:AreaTeleport(id:%llx  count:%d) packed_size:%lld", m_id, sum, packed_size);
				GameCore_SendPacket(0x2c3c, packedBuf.get(), packed_size);
			}
		}
	}
	//DEBUG_INFO_OUTPUT("fs:CGameData::CAreaTeleport 222\n");
	SetEvent();
}

void CGetTasks::UseCall()
{
	if (m_type == 0)
	{
		//DEBUG_INFO_OUTPUT("fs:CGameData::GetTaskAdventureExploration 1111\n");
		g_GameData.GetTaskAdventureExploration(m_tasks);
		//DEBUG_INFO_OUTPUT("fs:CGameData::GetTaskAdventureExploration 2222\n");
	}
	else
	{
		//DEBUG_INFO_OUTPUT("fs:CGameData::GetTaskDelegation 1111\n");
		g_GameData.GetTaskDelegation(m_tasks);
		//DEBUG_INFO_OUTPUT("fs:CGameData::GetTaskDelegation 2222\n");
	}
	SetEvent();
}

void CUseEquipment::UseCall()
{
	//DEBUG_INFO_OUTPUT("fs:CGameData::ScriptAPI_UseEquipment 1111\n");
	ScriptAPI_UseEquipment(m_id, m_index);
	//DEBUG_INFO_OUTPUT("fs:CGameData::ScriptAPI_UseEquipment 2222\n");
	SetEvent();
}

void CFindSWidget::UseCall()
{
	//DEBUG_INFO_OUTPUT("fs:CGameData::ScriptAPI_FindSWidget 1111\n");
	m_bFound = ScriptAPI_FindSWidget(m_text, m_rect);
	//DEBUG_INFO_OUTPUT("fs:CGameData::ScriptAPI_FindSWidget 2222\n");
	SetEvent();
}

void CUpdateTeleportPoint::UseCall()
{
	//DEBUG_INFO_OUTPUT("fs:CGameData::UpdateTeleportPoint 1111\n");
	g_GameData.UpdateTeleportPoint(m_TeleportPoints);
	//DEBUG_INFO_OUTPUT("fs:CGameData::UpdateTeleportPoint 2222\n");
	SetEvent();
}

void CUpdateSkill::UseCall()
{
	//DEBUG_INFO_OUTPUT("fs:CGameData::UpdateSkill 111\n");
	g_GameData.UpdateSkill(m_skills);
	//DEBUG_INFO_OUTPUT("fs:CGameData::UpdateSkill 222\n");
	SetEvent();
}

#include "useConsumable.pb-c.h"
void CUseConsumable::UseCall()
{
	//DEBUG_INFO_OUTPUT("fs:CGameData::CUseConsumable 111\n");
	IM__PUseConsumable item;
	im__puse_consumable__init(&item);
	item.toward = 0;
	item.itemid2 = m_itemID2;
	item.u = 1;
	IM__PUseConsumable__Pv itemPV = IM__PUSE_CONSUMABLE__PV__INIT;
	item.item = &itemPV;
	itemPV.itemid = m_itemID;
	item.destid = m_destID;
	UINT32 count = GameCore_read<UINT32>(g_TLMyPc + skillMessageCount);
	count++;
	GameCore_write<UINT32>(g_TLMyPc + skillMessageCount, count);
	item.count = count;
	IM__PUseConsumable__PPoint destPoint = IM__PUSE_CONSUMABLE__PPOINT__INIT;
	IM__PUseConsumable__PPoint selfPoint = IM__PUSE_CONSUMABLE__PPOINT__INIT;
	destPoint.x = m_destX;
	destPoint.y = m_destY;
	destPoint.z = m_destZ;
	selfPoint.x = g_GameData.m_LocalPlayerActor->m_x;
	selfPoint.y = g_GameData.m_LocalPlayerActor->m_y;
	selfPoint.z = g_GameData.m_LocalPlayerActor->m_z;
	item.destpoint = &destPoint;
	item.selfpoint = &selfPoint;

	size_t packed_size = protobuf_c_message_get_packed_size((const ProtobufCMessage*)&item);
	if (packed_size)
	{
		std::shared_ptr<uint8_t[]> packedBuf(new uint8_t[packed_size], std::default_delete<uint8_t[]>());
		if (packedBuf)
		{
			im__puse_consumable__pack(&item, packedBuf.get());
//#ifdef ENABLE_DEBUG_INFO_OUTPUT
//			std::string str = xorstr_("fs:");
//			str += byteArrayToHexString(packedBuf.get(), packed_size);
//			DEBUG_INFO_OUTPUT(str.c_str());
//#endif
			GameCore_SendPacket(0x120B, packedBuf.get(), packed_size);
		}
	}

	//DEBUG_INFO_OUTPUT("fs:CGameData::CUseConsumable 222\n");
	SetEvent();
}

#include "useConsumableWithQuantity.pb-c.h"
void CUseConsumableWithQuantity::UseCall()
{
	//DEBUG_INFO_OUTPUT("fs:CGameData::CUseConsumableWithQuantity 111\n");
	IM__PUseConsumableWithQuantity item;
	im__puse_consumable_with_quantity__init(&item);
	item.itemid = m_itemID;
	item.sum = m_sum;

	size_t packed_size = protobuf_c_message_get_packed_size((const ProtobufCMessage*)&item);
	if (packed_size)
	{
		std::shared_ptr<uint8_t[]> packedBuf(new uint8_t[packed_size], std::default_delete<uint8_t[]>());
		if (packedBuf)
		{
			im__puse_consumable_with_quantity__pack(&item, packedBuf.get());
			//#ifdef ENABLE_DEBUG_INFO_OUTPUT
			//			std::string str = "fs:";
			//			str += byteArrayToHexString(packedBuf.get(), packed_size);
			//			DEBUG_INFO_OUTPUT(str.c_str());
			//#endif
			GameCore_SendPacket(0x2850, packedBuf.get(), packed_size);
		}
	}
	//DEBUG_INFO_OUTPUT("fs:CGameData::CUseConsumableWithQuantity 222\n");
	SetEvent();
}

void CUpdateSchedule::UseCall()
{
	//DEBUG_INFO_OUTPUT("fs:CGameData::UpdateSchedule 1111\n");
	g_GameData.UpdateSchedule(m_SSchedules);
	//DEBUG_INFO_OUTPUT("fs:CGameData::UpdateSchedule 2222\n");
	SetEvent();
}

void CInteractive::UseCall()
{
	//DEBUG_INFO_OUTPUT("fs:CGameData::GameCore_Interactive 1111\n");
	GameCore_Interactive(m_actor_id);
	//DEBUG_INFO_OUTPUT("fs:CGameData::GameCore_Interactive 2222\n");
	SetEvent();
}

void CMovToActor::UseCall()
{
	GameCore_MovToActor(m_actor_id);
	SetEvent();
}

#include "setCharacterName.pb-c.h"
void CSetCharacterName::UseCall()
{
	//DEBUG_INFO_OUTPUT("fs:CGameData::CSetCharacterName 1111\n");
	IM__PSetCharacterName SetCharacterName;
	im__pset_character_name__init(&SetCharacterName);
	SetCharacterName.name = (char*)(m_name.c_str());

	size_t packed_size = protobuf_c_message_get_packed_size((const ProtobufCMessage*)&SetCharacterName);
	if (packed_size)
	{
		std::shared_ptr<uint8_t[]> packedBuf(new uint8_t[packed_size], std::default_delete<uint8_t[]>());
		if (packedBuf)
		{
			im__pset_character_name__pack(&SetCharacterName, packedBuf.get());
			GameCore_SendPacket(0x2910, packedBuf.get(), packed_size);
		}
	}
	//DEBUG_INFO_OUTPUT("fs:CGameData::CSetCharacterName 2222\n");
	SetEvent();
}

void CGetInventory::UseCall()
{
	//DEBUG_INFO_OUTPUT("fs:CGameData::UpdateInventory 1111\n");
	g_GameData.UpdateInventory(m_items);
	//DEBUG_INFO_OUTPUT("fs:CGameData::UpdateInventory 2222\n");
	SetEvent();
}

void CUpdateActorEffect::UseCall()
{
	//DEBUG_INFO_OUTPUT("fs:CGameData::UpdateActorEffect 1111\n");
	g_GameData.UpdateActorEffect(m_Effect);
	//DEBUG_INFO_OUTPUT("fs:CGameData::UpdateActorEffect 2222\n");
	SetEvent();
}

void CFacingCoordinates::UseCall()
{
	//DEBUG_INFO_OUTPUT("fs:CGameData::GameCore_FacingCoordinates 1111\n");
	GameCore_FacingCoordinates(g_GameData.m_LocalPlayerActor, m_p);
	//DEBUG_INFO_OUTPUT("fs:CGameData::GameCore_FacingCoordinates 2222\n");
	SetEvent();
}

void CToggleKeepMoveForward::UseCall()
{
	GameCore_ToggleKeepMoveForward();
	SetEvent();
}

#include "inviteToTeam.pb-c.h"
void CInviteToTeam::UseCall()
{
	IM__PInviteToTeam inviteToTeam;
	im__pinvite_to_team__init(&inviteToTeam);
	inviteToTeam.name = (char*)(m_name.c_str());

	size_t packed_size = protobuf_c_message_get_packed_size((const ProtobufCMessage*)&inviteToTeam);
	if (packed_size)
	{
		std::shared_ptr<uint8_t[]> packedBuf(new uint8_t[packed_size], std::default_delete<uint8_t[]>());
		if (packedBuf)
		{
			im__pinvite_to_team__pack(&inviteToTeam, packedBuf.get());
			GameCore_SendPacket(0x2e00, packedBuf.get(), packed_size);
		}
	}

	SetEvent();
}

#include "teleport_sbww.pb-c.h"
void CTeleport_sbww::UseCall()
{
	int sum = GameCore_read<int>(GameCore_GetGameBaseAddress() + AreaTeleportSum_BASE);
	sum++;
	if (sum < 0)
	{
		sum = 1;
	}
	GameCore_write<int>(GameCore_GetGameBaseAddress() + AreaTeleportSum_BASE, sum);

	IM__PTeleportSbww teleportSbww;
	im__pteleport_sbww__init(&teleportSbww);
	teleportSbww.count = sum;

	size_t packed_size = protobuf_c_message_get_packed_size((const ProtobufCMessage*)&teleportSbww);
	if (packed_size)
	{
		std::shared_ptr<uint8_t[]> packedBuf(new uint8_t[packed_size], std::default_delete<uint8_t[]>());
		if (packedBuf)
		{
			im__pteleport_sbww__pack(&teleportSbww, packedBuf.get());
			GameCore_SendPacket(0x2c16, packedBuf.get(), packed_size);
		}
	}
	SetEvent();
}

void CGetPartyInfo::UseCall()
{
	g_GameData.UpdatePartyInfo(m_partyInfoS);
	SetEvent();
}
